//
//  ZJUserInfoModel.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/15.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJUserInfoModel.h"

static ZJUserInfoModel *userInfo;

@implementation ZJUserInfoModel




+ (ZJUserInfoModel *)sharedUserInfo{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        userInfo = [[ZJUserInfoModel alloc]init];
        if ([[NSUserDefaults standardUserDefaults] objectForKey:USER_INFO]) {
    //        NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:USER_INFO]);
           userInfo = [ZJUserInfoModel yy_modelWithJSON:[self jsonStringToObject:[[NSUserDefaults standardUserDefaults] objectForKey:USER_INFO]]];
        }
    });
    return userInfo;
}

+ (void)updateUserInfoFormNSUserDefaults{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:USER_INFO]) {
        userInfo = [ZJUserInfoModel yy_modelWithJSON:[self jsonStringToObject:[[NSUserDefaults standardUserDefaults] objectForKey:USER_INFO]]];
    }else{
        userInfo = nil;
    }

}

+ (void)updateUserInfoToNSUserDefaults{
    NSDictionary *dict = [userInfo yy_modelToJSONObject];
    [[NSUserDefaults standardUserDefaults] setObject:[dict yy_modelToJSONString] forKey:USER_INFO];
}

- (NSString *)description{
    
    return [self yy_modelDescription];
}


- (NSMutableArray<ZJUserModel *> *)ORG_USER_MODEL{
    _ORG_USER_MODEL = [[NSArray yy_modelArrayWithClass:[ZJUserModel class] json:self.ORG_USER] mutableCopy];
   
    return _ORG_USER_MODEL;
}


/**  书记判断  */
- (BOOL)isSecretary{
    for (ZJRoleInfoModel *role in self.ORG_ROLE_MODEL) {
        if ([role.ROLE_CODE isEqualToString:@"1"]) {
            return YES;
        }
    }
    return NO;
}

- (NSMutableArray<ZJRoleInfoModel *> *)ORG_ROLE_MODEL{
    _ORG_ROLE_MODEL = [[NSArray yy_modelArrayWithClass:[ZJRoleInfoModel class] json:self.ORG_ROLE] mutableCopy];
    
    return _ORG_ROLE_MODEL;
}

- (NSMutableArray<ZJRoleInfoModel *> *)secretary_Role_Model_Array{
    NSMutableArray *array = [NSMutableArray array];
    for (ZJRoleInfoModel *role  in self.ORG_ROLE_MODEL) {
        if ([role.ROLE_CODE isEqualToString:@"1"]) {
            [array addObject:role];
        }
    }
    return array;
}

- (ZJRoleInfoModel *)CURRENT_ROLE_MODEL{
    NSInteger number = 0;
    NSInteger index = 0;
    if (self.secretary_Role_Model_Array.count != 0) {
        for (int i = 0; i < self.secretary_Role_Model_Array.count; i ++) {
            ZJRoleInfoModel *model = self.secretary_Role_Model_Array[i];
            if (i == 0) {
                number = [model.SORT integerValue];
                index = i;
            }else{
                if ([model.SORT integerValue] < number) {
                    number = [model.SORT integerValue];
                    index = i;
                }
                
                
            }
        }
        return self.secretary_Role_Model_Array[index];
    }else{
        return nil;
    }
    
}



- (NSString *)secretaryOrg{
    
    if (self.isSecretary) {
        for (ZJDeptModel *dept  in self.DEPT_MODEL ) {
            if ([dept.ID isEqualToString:self.CURRENT_ROLE_MODEL.DEPT_ID]) {
                return dept.DEPTABBREVIATION;
            }
        }
    }else{
        if (self.DEPT_MODEL_BOOK) {
             return self.DEPT_MODEL_BOOK.DEPTABBREVIATION;
        }
       
    }
  
    return nil;
    
}


- (ZJDeptModel *)CURRENT_BRANCH_MODEL{
    if (self.isSecretary) {
        for (ZJDeptModel *dept  in self.DEPT_MODEL ) {
            if ([dept.ID isEqualToString:self.CURRENT_ROLE_MODEL.DEPT_ID]) {
                return dept;
            }
        }
    }else{
        if (self.DEPT_MODEL_BOOK) {
            return self.DEPT_MODEL_BOOK;
        }
    }
    return nil;
}


- (ZJPartymember *)ORG_PARTYMEMBER_MODEL{
    return [ZJPartymember yy_modelWithJSON:self.ORG_PARTYMEMBER];
}

- (ZJDeptModel *)ORG_ZC_DEPT_FirstModel{
    if (self.ORG_ZC_DEPT.count > 0) {
        return [ZJDeptModel yy_modelWithJSON:self.ORG_ZC_DEPT.firstObject];
    }
    return nil;
}

- (NSMutableArray<ZJDeptModel *> *)DEPT_MODEL{
    _DEPT_MODEL = [[NSArray yy_modelArrayWithClass:[ZJDeptModel class] json:self.DEPT] mutableCopy];
    return _DEPT_MODEL;
}


- (ZJUserModel *)CURRENT_USER_MODEL{
    
    if (self.USER_MODEL_BOOK) {
        _CURRENT_USER_MODEL = self.USER_MODEL_BOOK;
    }else{
        _CURRENT_USER_MODEL = self.USER_MODEL_JOB;
    }
    return _CURRENT_USER_MODEL;
}

- (ZJUserModel *)USER_MODEL_JOB{
    return self.USER_MODEL_BOOK;
}

- (ZJUserModel *)USER_MODEL_BOOK{
    for (int i = 0; i < self.ORG_USER_MODEL.count; i ++) {
        ZJUserModel *userModel = self.ORG_USER_MODEL[i];
        if ([userModel.USER_TYPE isEqualToString:@"3"]) {
            return userModel;
        }
    }
    return nil;
}

- (ZJDeptModel *)CURRENT_DEPT_MODEL{
    
    if (self.DEPT_MODEL_BOOK) {
        _CURRENT_DEPT_MODEL = self.DEPT_MODEL_BOOK;
    }else{
        _CURRENT_DEPT_MODEL = self.DEPT_MODEL_JOB;
       
    }
    return _CURRENT_DEPT_MODEL;
}

- (ZJDeptModel *)DEPT_MODEL_JOB{
    return self.DEPT_MODEL_BOOK;
}

- (ZJDeptModel *)DEPT_MODEL_BOOK{
    for (int i = 0; i < self.DEPT.count; i ++) {
        ZJDeptModel * deft = self.DEPT_MODEL[i];
        if ([deft.ID isEqualToString:self.USER_MODEL_BOOK.UNITID]) {//用户表中的UNITID来找dept表
            _DEPT_MODEL_BOOK = deft;
            if (![self.USER_MODEL_BOOK.AUTH_STATUS isEqualToString:@"2"]) {//审核不通过
                _DEPT_MODEL_BOOK.ID = @"";
            }
            return _DEPT_MODEL_BOOK;
        }
    }
    return nil;
}

@end

@implementation ZJUserModel

- (NSString *)description{
    
    return [self yy_modelDescription];
}
@end

@implementation ZJRoleInfoModel

- (NSString *)description{
    
    return [self yy_modelDescription];
}

@end

@implementation ZJAccountInfo

- (NSString *)description{
    
    return [self yy_modelDescription];
}

- (void)setUSER_NAME:(NSString *)USER_NAME{
    _USER_NAME = USER_NAME;
    
}
//+ (NSDictionary *)modelCustomPropertyMapper{
//    
//    return @{@"PARTY_MEMBER_ID":@"PARTYMEMBER_ID"};
//}

@end

@implementation ZJUnitInfoModel

- (NSString *)description{
    
    return [self yy_modelDescription];
}

@end

@implementation ZJDeptModel

- (NSString *)description{
    
    return [self yy_modelDescription];
}
@end

@implementation ZJPartymember

- (NSString *)description{
    
    return [self yy_modelDescription];
}
@end
